package com.yanfan.dlt698_45.frame;


import com.yanfan.dlt698_45.code.AddressType;
import com.yanfan.dlt698_45.code.DataTypeCode;
import com.yanfan.dlt698_45.exception.DLT698_45TransportException;
import io.netty.buffer.ByteBuf;

/**
 * @author oliver foo
 * @description
 * @date 2023/3/21 14:58
 */
public class SecurityGetNormalResponse extends SecurityResponse {
    // 通信访问地址，也就是读取数据的属性
    public byte[] objectAddressDesc;
    private Object data = null;
    private int priorityId = 1;


    public SecurityGetNormalResponse(byte[] address, byte addressType) throws DLT698_45TransportException {
        super(address, addressType);
        this.objectAddressDesc = new byte[4];
    }


    public SecurityGetNormalResponse(byte[] address) throws DLT698_45TransportException {
        super(address, AddressType.SINGLE_ADD);
        this.objectAddressDesc = new byte[4];
    }


    @Override
    int getFrameLength() {
        return 0;
    }

    @Override
    protected void writeImpl(ByteBuf buf) {

    }


    @Override
    protected void readResponse(ByteBuf buf) {
        //明文的长度
        short frameLength = buf.readUnsignedByte();
        //FrameType.GET_RESPONSE  0x85
        byte frame = buf.readByte();
        // FrameType.GET_RESPONSE_NORMAL 0x01
        byte noraml = buf.readByte();

        this.priorityId = buf.readByte();
        // 通信访问地址，也就是读取数据的属性
        buf.readBytes(objectAddressDesc);

        /**
         * result  0  错误   1 成功
         */
        byte result = buf.readByte();
        if (result == 0x00) {
            setException(true);
            setError(buf.readByte());
        } else {
            readData(buf);

        }
    }

    private void readData(ByteBuf buf) {
        byte dataType = buf.readByte();
        switch (dataType) {
            case DataTypeCode.NULL:
                this.data = 0;
                break;
            case DataTypeCode.BIT_STRING:
                byte count = buf.readByte();
                this.data = new byte[count];
                buf.readBytes((byte[]) this.data);
                break;
            case DataTypeCode.INTEGER:
                this.data = buf.readByte();
                break;
            case DataTypeCode.UNSIGNED:
                this.data = buf.readUnsignedByte();
                break;
            case DataTypeCode.LONG:
                this.data = buf.readShort();
                break;
            case DataTypeCode.VISIBLE_STRING:
            case DataTypeCode.UTF8_STRING:
            case DataTypeCode.LONG_UNSIGNED:
                this.data = buf.readUnsignedShort();
                break;
            case DataTypeCode.DOUBLE_LONG:
                this.data = buf.readInt();
                break;
            case DataTypeCode.DOUBLE_LONG_UNSIGNED:
                this.data = buf.readUnsignedInt();
                break;
            case DataTypeCode.LONG64:
            case DataTypeCode.LONG64_UNSIGNED:
                this.data = buf.readLong();
                break;
            case DataTypeCode.FLOAT32:
                this.data = buf.readFloat();
                break;
            case DataTypeCode.FLOAT64:
                this.data = buf.readDouble();
                break;


        }
    }

    public int getPriorityId() {
        return priorityId;
    }

    @Override
    public byte[] getObjectAddressDesc() {
        return objectAddressDesc;
    }

    @Override
    public Object getData() {
        return data;
    }
}
